package cn.itsource.service.remoteOpenfeign;

import cn.itsource.domain.BillDto;
import cn.itsource.domain.DriverSummaryInfo;
import cn.itsource.mapper.*;
import cn.itsource.openfeign.DriverSummaryOpenFeign;
import cn.itsource.openfeign.ValuationOpenFeign;
import cn.itsource.pojo.domain.*;
import cn.itsource.result.JSONResult;
import cn.itsource.utils.RConst;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @program fccar_tt
 * @Description
 * @Author TT
 * @Date 2024/09/21 12:41
 * @Version 1.0
 */
@RestController
public class ValuationImpl implements ValuationOpenFeign {
    @Autowired
    private ChargeRuleStartMapper chargeRuleStartMapper;
    @Autowired
    private ChargeRuleReturnMapper chargeRuleReturnMapper;
    @Autowired
    private ChargeRuleWaitMapper chargeRuleWaitMapper;
    @Autowired
    private ProfitsharingRuleBaseMapper profitsharingRuleBaseMapper;
    @Autowired
    private ProfitsharingRuleDeductCancelMapper profitsharingRuleDeductCancelMapper;
    @Autowired
    private ProfitsharingRuleDeductComplaintMapper profitsharingRuleDeductComplaintMapper;
    @Autowired
    private DriverSummaryOpenFeign driverSummaryOpenFeign;

    /**
     * 获取预计里程费
     * */
    @Override
    public JSONResult getValuation(String actualMileage) {
        int hour = LocalDateTime.now().getHour();
        //查询在大于等于规定的开始规则时间，小于结束规则时间的具体规则对象
        ChargeRuleStart chargeRuleStart = chargeRuleStartMapper.selectOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .le(ChargeRuleStart::getHourStart, hour)
                .gt(ChargeRuleStart::getHourEnd, hour));
        //获取基础里程
        BigDecimal baseMileage = chargeRuleStart.getBaseMileage();
        //将传入的实际里程数转换成BigDecimal
        BigDecimal mileage = new BigDecimal(actualMileage);
        //如果当前对象小于参数对象，返回一个负整数。
        //如果当前对象等于参数对象，返回0。
        //如果当前对象大于参数对象，返回一个正整数
        int i = baseMileage.compareTo(mileage);
        if(i < 0){
            //计算出超出的里程价钱
            BigDecimal exceeding = mileage.subtract(baseMileage)
                                          .multiply(chargeRuleStart.getExceedEveryKmAmount());
            //超出的加上基础价钱
            BigDecimal endValuation = exceeding.add(chargeRuleStart.getAmount());
            return JSONResult.success(endValuation);
        }
        return JSONResult.success(chargeRuleStart.getAmount());
    }

    @Override
    public JSONResult getBillDto(Integer hour) {
        BillDto billDto = new BillDto();
        //查询在大于等于规定的开始规则时间，小于结束规则时间的具体规则对象
        ChargeRuleStart chargeRuleStart = chargeRuleStartMapper.selectOne(new LambdaQueryWrapper<ChargeRuleStart>()
                .le(ChargeRuleStart::getHourStart, hour)
                .gt(ChargeRuleStart::getHourEnd, hour));
        billDto.setBaseMileage(chargeRuleStart.getBaseMileage());
        billDto.setBaseMileageAmount(chargeRuleStart.getAmount());
        billDto.setExceedBaseMileageEveryKmAmount(chargeRuleStart.getExceedEveryKmAmount());
        //----------------------------------------------------------------------
        ChargeRuleWait chargeRuleWait = chargeRuleWaitMapper.selectOne(new LambdaQueryWrapper<ChargeRuleWait>()
                .eq(ChargeRuleWait::getId, RConst.Num.INT_1));
        billDto.setFreeBaseWaitingMinute(chargeRuleWait.getFreeBaseWaitingMinute());
        billDto.setExeceedBaseWaitingEveryKmAmount(chargeRuleWait.getExceedEveryMinuteAmount());
        //----------------------------------------------------------------------
        ChargeRuleReturn chargeRuleReturn = chargeRuleReturnMapper.selectOne(new LambdaQueryWrapper<ChargeRuleReturn>()
                .eq(ChargeRuleReturn::getId, RConst.Num.INT_1));
        billDto.setFreeBaseReturnMileage(chargeRuleReturn.getFreeBaseReturnMileage());
        billDto.setExceedBaseReturnEveryKmAmount(chargeRuleReturn.getExceedEveryKmAmount());
        return JSONResult.success(billDto);
    }

    /**
     * 获取平台比例
     * */
    @Override
    public JSONResult getProportion() {
        //获取到司机的当日
        JSONResult result = driverSummaryOpenFeign.getDriverSummaryInfo();
        //转换为对象
        DriverSummaryInfo driverSummaryInfo =
                JSONObject.parseObject(JSONObject.toJSONString(result.getData()), DriverSummaryInfo.class);
        //获取默认的抽成比例
        ProfitsharingRuleBase profitsharingRuleBase = profitsharingRuleBaseMapper.selectById(RConst.Num.INT_1);
        BigDecimal defaultProportion = profitsharingRuleBase.getDriverRatio();
        //获取取消订单数量范围内平台的抽成
        ProfitsharingRuleDeductCancel cancel = profitsharingRuleDeductCancelMapper
                .selectOne(new LambdaQueryWrapper<ProfitsharingRuleDeductCancel>()
                        .le(ProfitsharingRuleDeductCancel::getNumberFrom, driverSummaryInfo.getTodayCancel())
                        .gt(ProfitsharingRuleDeductCancel::getNumberTo, driverSummaryInfo.getTodayCancel()));
        if(cancel!=null){
            //计算平台的总抽成
            defaultProportion = defaultProportion.subtract(cancel.getDriverRatioDeduct());
        }
        //获取投诉订单数量范围内平台的抽成
        ProfitsharingRuleDeductComplaint complaint = profitsharingRuleDeductComplaintMapper
                .selectOne(new LambdaQueryWrapper<ProfitsharingRuleDeductComplaint>()
                        .le(ProfitsharingRuleDeductComplaint::getNumberFrom, driverSummaryInfo.getTodayComplaint())
                        .gt(ProfitsharingRuleDeductComplaint::getNumberTo, driverSummaryInfo.getTodayComplaint()));
        if(complaint!=null){
            //计算平台的总抽成
            defaultProportion = defaultProportion.subtract(complaint.getDriverRatioDeduct());
        }

        return JSONResult.success(defaultProportion);
    }


}
